<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\business\BusinessContract;
use yii\helpers\ArrayHelper;
use \kartik\date\DatePicker;
use kartik\file\FileInput;
use backend\assets\RotateAsset;
RotateAsset::register($this);
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessContract */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="business-contract-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>
                        <?= $form->field($model, 'cateid')->dropDownList(ArrayHelper::map(BusinessContract::Allcate(),'id','title')) ?>

                        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'no')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'displayorder')->textInput() ?>

                        <?= $form->field($model, 'partyA')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'partyB')->textInput(['maxlength' => true]) ?>

                    <?php if(BusinessContract::isShow('amount',$showpre)) { ?>
                        <?= $form->field($model, 'amount')->textInput(['maxlength' => true]) ?>
                    <?php } ?>

                    <?php if(BusinessContract::isShow('num',$showpre)) { ?>
                        <?= $form->field($model, 'num')->textInput(['maxlength' => true]) ?>
                    <?php } ?>

                    <?php if(BusinessContract::isShow('advance_ratio',$showpre)) { ?>
                        <?= $form->field($model, 'advance_ratio')->textInput(['maxlength' => true]) ?>
                    <?php } ?>

                        <?=
                            $form->field($model, 'start_time')->widget(DatePicker::classname(), [
                                'options' => ['placeholder' => '选择合同生效时间'],
                                'pluginOptions' => [
                                    'autoclose'=>true,
                                    'format' => 'yyyy-mm-dd'
                                ]
                            ]);
                        ?>
                        <?=
                        $form->field($model, 'end_time')->widget(DatePicker::classname(), [
                            'options' => ['placeholder' => '选择合同失效时间'],
                            'pluginOptions' => [
                                'autoclose'=>true,
                                'format' => 'yyyy-mm-dd'
                            ]
                        ]);
                        ?>
                    <?php if(BusinessContract::isShow('final_amount',$showpre)) { ?>
                        <?= $form->field($model, 'final_amount')->textInput(['maxlength' => true]) ?>
                    <?php } ?>
                    <?php if(BusinessContract::isShow('final_num',$showpre)) { ?>
                        <?= $form->field($model, 'final_num')->textInput() ?>
                    <?php } ?>
                    <?php if(BusinessContract::isShow('address',$showpre)) { ?>
                        <?= $form->field($model, 'address')->textInput() ?>
                    <?php } ?>
                        <?= $form->field($model, 'afile[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->afile,true,'afile',false),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'afile',\common\models\business\BusinessContract::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                                'maxFileSize' => 5000,
                                'previewZoomButtonIcons' => [
                                    'prev'=> '<i class="glyphicon glyphicon-triangle-left"></i>',
                                    'next'=> '<i class="glyphicon glyphicon-triangle-right"></i>',
                                    'toggleheader'=> '<i class="glyphicon glyphicon-resize-vertical"></i>',
                                    'fullscreen'=> '<i class="glyphicon glyphicon-fullscreen"></i>',
                                    'borderless'=> '<i class="glyphicon glyphicon-resize-full"></i>',
                                    'close'=> '<i class="glyphicon glyphicon-remove"></i>',
                                ]
                            ],
                        ]); ?>
                    <?php if(BusinessContract::isShow('aopenbank',$showpre)) { ?>
                        <?= $form->field($model, 'aopenbank')->textInput() ?>
                    <?php } ?>

                    <?php if(BusinessContract::isShow('aopenbankadd',$showpre)) { ?>
                        <?= $form->field($model, 'aopenbankadd')->textInput() ?>
                    <?php } ?>

                    <?php if(BusinessContract::isShow('aopenbankaccount',$showpre)) { ?>
                        <?= $form->field($model, 'aopenbankaccount')->textInput() ?>
                    <?php } ?>
                        <!--附加字段-->
                        <?php if($model->addonField) { ?>
                            <?php foreach($model->addonField as $key => $row) { ?>
                                <div class="form-group field-businesscontract-"<?= $row->name?>>
                                    <div class="col-xs-3 col-sm-2 text-right"><label class="control-label" for="businesscontract-"<?= $row->name?>><?= $row->name?></label></div><div class="col-xs-9 col-sm-7"><input type="text" id="businesscontract-"<?= $row->name?> class="form-control" name="<?= $row->name?>" value="<?= \common\models\business\BusinessContract::fieldValue($model->id,$row->id)?>" aria-required="true" aria-invalid="false"></div><div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0"><div class="help-block"></div></div>
                                </div>
                            <?php } ?>
                        <?php } ?>

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>


<script>
    <?php $this->beginBlock('js_end') ?>


    $('#businesscontract-cateid').change(function () {
        window.location.href = '/business/contract/create?cateid='+$(this).val();
    });

    //自动生成排序
    $('#businesscontract-no').blur(function(){
        $('#businesscontract-displayorder').val($(this).val().replace(/[^0-9]/ig,""))
    });

    //旋转图片
    $('#businesscontract-afile').on('filezoomshow', function(event, params) {
        var str = '<button type="button" id="remoat" class="btn btn-default btn-borderless" title="旋转" data-toggle="button" aria-pressed="false" autocomplete="off"><i class="glyphicon glyphicon-refresh"></i>旋转</button>'
        $('.btn-borderless').after(str);
        var num = 0;
        $('#remoat').click(function(){
            num ++;
           $('.file-zoom-content').rotate(90*num);
        });
        console.log('File zoom show ', params.sourceEvent, params.previewId, params.modal);
    });


    //排序图片
    $('.field-businesscontract-afile').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>
